#include "../Source/server.hpp"

void TestBuffer()
{
    Buffer bf;
    char res[1280];

    char data[] = "1234567erwre3432222222222289";
    bf.Write(data,sizeof(data)-1);
    char data1[] = "1234567erwre3432222222222289avdsad||";
    bf.Write(data1,sizeof(data1)-1);
    //bf.Read(res,sizeof(data) + sizeof(data1) - 2);
    //cout << res << endl;

    cout << "-----------------------------" << endl;

    Buffer bf1;
    memset(res,0,sizeof(res));
    string s ("test !!!! ||");
    bf1.WriteString(s);

    // bf1.Read(res, s.size());
    // cout << res << endl;   

    cout << bf1.ReadString(s.size()) << endl;

    cout << "-----------------------------" << endl;
    Buffer bf2;
    memset(res,0,sizeof(res));
    bf2.WriteBuffer(bf);
    bf2.WriteBuffer(bf2);       //支持“自己写自己”
    bf2.Read(res,100);
    cout << res << endl;

    cout << "-----------------------------" << endl;
    Buffer bf3;
    memset(res, 0, sizeof(res));
    char url[] = "GET /bitejiuyeke?a=b&c=d HTTP/1.1\r\n";
    bf3.Write(url,sizeof(url));
    cout << bf3.GetLine() << endl;
}


void TestComplie()
{
    Poller p;
    Channel c(1,nullptr);
}

void TimerFdTest()
{
    //1. 获取fd
    int fd1 = timerfd_create(CLOCK_MONOTONIC, 0);

    //2. 进行超时时间的设置
    itimerspec new_val1;
    new_val1.it_value.tv_sec = 1;
    new_val1.it_value.tv_nsec = 0;
    new_val1.it_interval.tv_sec = 1;
    new_val1.it_interval.tv_nsec = 0;
    int ret = timerfd_settime(fd1, 0, &new_val1, nullptr);
    if(ret < 0)
    {
        ERR_LOG("timerfd_settime failed: %s",strerror(errno));
    }

    //3. 读取超时次数
    uint64_t buff = 0;
    while(1)
    {
        sleep(5);
        int n = read(fd1, &buff, sizeof(buff));
        if(n > 0)
        {
            cout << "expiration times: " << buff << endl;
        }        
        if(buff > 5)
        {
            break;
        }
    }

    //4. 关闭文件描述符
    close(fd1);
}

#define TIMER_SEC 1 // 定时器设置为5秒  
  
void TimerEpollTest() 
{  
    int epoll_fd, timer_fd, nfds;  
    struct epoll_event ev, events[1];  
    struct itimerspec its;  
  
    // 创建 epoll 文件描述符  
    epoll_fd = epoll_create(1024);  
  
    // 创建 timerfd  
    timer_fd = timerfd_create(CLOCK_REALTIME, 0);  

    // 设置定时器  
    memset(&its, 0, sizeof(struct itimerspec));  
    its.it_value.tv_sec = TIMER_SEC; // 首次触发时间  
    its.it_interval.tv_sec = TIMER_SEC; // 间隔时间  
    if (timerfd_settime(timer_fd, 0, &its, NULL) == -1) {  
        perror("timerfd_settime");  
        exit(EXIT_FAILURE);  
    }  
  
    // 注册 timerfd 到 epoll  
    ev.events = EPOLLIN;  
    ev.data.fd = timer_fd;  
    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, timer_fd, &ev) == -1) {  
        perror("epoll_ctl: add");  
        exit(EXIT_FAILURE);  
    }  
  
    // 等待事件  
    while(1)
    {
        nfds = epoll_wait(epoll_fd, events, 1, -1);  
        if (nfds == -1) {  
            perror("epoll_wait");  
            exit(EXIT_FAILURE);
        }  
    
        // 处理事件（这里是读取定时器到期的通知）  
        if (events[0].data.fd == timer_fd) {  
            uint64_t expirations;  
            ssize_t s = read(timer_fd, &expirations, sizeof(expirations));  

            printf("Timer expired %llu times\n", (unsigned long long)expirations);  
        }          
    }

  
    // 清理  
    close(timer_fd);  
    close(epoll_fd);  
}

int main()
{
    //TestBuffer();
    //TestComplie();
    //TimerFdTest();
    TimerEpollTest();
    return 0;
}